package com.adtec.base.cloud.baseservice.rbac.system.controller;

import com.adtec.base.cloud.baseservice.rbac.system.model.UserRoleCheckedIds;
import com.adtec.base.cloud.baseservice.rbac.system.service.SysRoleService;
import com.adtec.base.cloud.commons.exception.AjaxResponse;
import com.adtec.base.cloud.persistence.system.model.SysRole;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sysrole")
@Tag(name = "sysrole", description = "角色管理")
public class SysRoleController {

    @Resource
    private SysRoleService sysroleService;

    //角色管理:查询
    @PostMapping(value = "/query")
    @Operation(description = "角色管理:查询")
    @Parameters({
            @Parameter(name = "roleLike", description = "角色(模糊查询)"),
    })
    public List<SysRole> query(@RequestParam("roleLike") String roleLike) {
        return sysroleService.queryRoles(roleLike);
    }

    //角色管理：修改
    @PostMapping(value = "/update")
    @Operation(description = "角色管理:修改")
    public AjaxResponse update(@RequestBody SysRole sysRole) {
        sysroleService.updateRole(sysRole);
        return AjaxResponse.success("更新角色成功！");
    }

    //角色管理：新增
    @PostMapping(value = "/add")
    @Operation(description = "角色管理:新增")
    public AjaxResponse add(@RequestBody SysRole sysRole) {
        sysroleService.addRole(sysRole);
        return AjaxResponse.success("新增角色成功！");
    }

    //角色管理：删除
    @PostMapping(value = "/delete")
    @Operation(description = "角色管理:删除")
    public AjaxResponse delete(@RequestParam Long roleId) {
        sysroleService.deleteRole(roleId);
        return AjaxResponse.success("删除角色成功!");
    }

    //用户管理：为用户分配角色，展示角色列表及勾选角色列表
    @PostMapping(value = "/checkedroles")
    @Operation(description = "用户管理：为用户分配角色，展示角色列表及勾选角色列表")
    @Parameters({
            @Parameter(name = "userId", description = "用户ID"),
    })
    public Map<String, Object> checkedroles(@RequestParam Long userId) {
        return sysroleService.getRolesAndChecked(userId);
    }

    //用户管理：保存用户角色
    @PostMapping(value = "/savekeys")
    @Operation(description = "用户管理：保存用户角色")
    public AjaxResponse savekeys(@RequestBody UserRoleCheckedIds userRoleCheckedIds) {
        sysroleService.saveCheckedKeys(
                userRoleCheckedIds.getUsername(),
                userRoleCheckedIds.getUserId(),
                userRoleCheckedIds.getCheckedIds()
        );
        return AjaxResponse.success("保存用户角色成功!");
    }


    //角色管理：更新角色禁用状态
    @PostMapping(value = "/status/change")
    @Operation(description = "角色管理：更新角色禁用状态")
    @Parameters({
            @Parameter(name = "roleId", description = "角色ID"),
            @Parameter(name = "status", description = "角色状态"),
    })
    public AjaxResponse update(@RequestParam Long roleId,
                               @RequestParam Boolean status) {
        sysroleService.updateStatus(roleId, status);
        return AjaxResponse.success("角色禁用状态更新成功！");
    }
}
